﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public partial class CurveData
{
    Vector3 axis;
    Quaternion q;
    Vector3Norm to, from;
    float angle;
    void Snap(Transform t, Vector3 to)
    {
        t.localRotation = Quaternion.identity; // 重置旋转确保接下来的旋转操作正确

        //Snap1(t, to);
        //Snap2(t, to);
        //Snap3(t, to);
        SnapL(t, to);
    }
    public void SnapA(Transform t, Vector3 toPos, Vector3 axis)
    {
        from = t.up;
        to = toPos;
        angle = Vector3.SignedAngle(from, toPos, axis);
        q = Quaternion.AngleAxis(angle, axis);
        t.rotation = q * t.rotation;
    }
    void SnapL(Transform t, Vector3 toPos)
    {
        t.rotation = Quaternion.FromToRotation(t.up, toPos.normalized) * t.rotation;
    }
    /// arbitrarily axis
    void Snap1(Transform t, Vector3 toPos)
    {
        t.rotation = Quaternion.FromToRotation(t.up, toPos.normalized) * t.rotation;
    }
    public void Snap3(Transform t, Vector3 toPos)
    {
        from = t.up;
        to = toPos;
        axis = t.right;
        angle = Vector3.SignedAngle(from, to, axis);
        q = Quaternion.AngleAxis(angle, axis);
        t.rotation = q * t.rotation;
    }
    void Snap2(Transform t, Vector3 toPos)
    {
        ///overlook
        to = toPos.toX0Z();
        from = t.up.toX0Z();
        axis = Vector3.up;
        angle = Vector3.SignedAngle(from, to, axis);
        q = Quaternion.AngleAxis(angle, axis);
        t.rotation = q * t.rotation;

        //continue;
        ///sideface
        to = toPos;
        from = t.up;
        axis = t.right;
        angle = Vector3.SignedAngle(from, to, axis);
        q = Quaternion.AngleAxis(angle, axis);
        t.rotation = q * t.rotation;
    }
}
